<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-export-btn :columns="Table?.columns" />

			<el-button type="danger" @click="cleanData">数据清洗</el-button>
			<el-button type="primary" @click="mergeEyeData">数据合并</el-button>
			<cl-flex1 />
			<!-- 条件搜索 -->
			<cl-search ref="Search" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" setup>
defineOptions({
	name: "student-checkData",
});

import { useCrud, useTable, useUpsert, useSearch } from "@cool-vue/crud";
import { useCool } from "/@/cool";
import { useI18n } from "vue-i18n";
import { reactive } from "vue";
import { useDict } from '/$/dict';

const { service } = useCool();
const { t } = useI18n();
const { dict } = useDict();

// 选项
const options = reactive({
	bodyPart: [
		{ label: t("左眼"), value: 0, type: "danger" },
		{ label: t("右眼"), value: 1, type: "success" },
	],
	riseOrFall: [
		{ label: t("首次"), value: 0, type: "primary" },
		{ label: t("上升"), value: 1, type: "danger" },
		{ label: t("下降"), value: 2, type: "success" },
		{ label: t("不变"), value: 4, type: "info" },
	]
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: t("选择学员"),
			prop: "studentId",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
			required: true,
		},
		{
			label: t("诊疗号"),
			prop: "treatmentNumber",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("选择门店"),
			prop: "shopId",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("左右眼"),
			prop: "bodyPart",
			component: { name: "el-radio-group", options: options.bodyPart },
			value: 0,
			required: true,
		},
		{
			label: t("检查数据"),
			prop: "checkData",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
			required: true,
		},
		{
			label: t("检查时间"),
			prop: "checkTime",
			component: {
				name: "el-date-picker",
				props: { type: "datetime", valueFormat: "YYYY-MM-DD HH:mm:ss" },
			},
			span: 12,
			required: true,
		},
		{
			label: t("选择预约"),
			prop: "appointmentId",
			component: { name: "el-input", props: { clearable: true } },
			span: 12,
		},
		{
			label: t("删除时间"),
			prop: "deleteDate",
			component: {
				name: "el-date-picker",
				props: { type: "date", valueFormat: "YYYY-MM-DD" },
			},
			span: 12,
		},
	],
});

// cl-table
const Table = useTable({
	columns: [
		{ label: t("姓名"), prop: "studentName", minWidth: 80 },
		{ label: t("手机号"), prop: "studentPhone", minWidth: 120 },
		{
			label: t("左眼变化"),
			prop: "riseOrFall",
			minWidth: 80,
			dict: options.riseOrFall,
		},
		{ label: t("左眼数据"), prop: "checkData", minWidth: 120 },
		{
			label: t("左眼差值"),
			prop: "riseOrFallValue",
			minWidth: 120,
		},
		{
			label: t("右眼变化"),
			prop: "riseOrFall",
			minWidth: 80,
			dict: options.riseOrFall,
		},
		{ label: t("右眼数据"), prop: "checkData", minWidth: 120 },
		{
			label: t("右眼差值"),
			prop: "riseOrFallValue",
			minWidth: 120
		},
		// 检查次数
		{ label: t("检查次数"), prop: "checkTimes", minWidth: 100 },
		{ label: t("间隔天数"), prop: "checkInterval", minWidth: 100 },
		{
			label: t("检查时间"),
			prop: "checkTime",
			minWidth: 170,
			sortable: "custom",
			component: { name: "cl-date-text" },
		},
		{
			label: t('入组'),
			prop: 'inGroup',
			minWidth: 100,
		},
		// 成单金额
		{ label: t("消费金额"), prop: "totalPrice", minWidth: 120 },
		// 购买产品
		{ label: t("购买产品"), prop: "productName", minWidth: 140, showOverflowTooltip: true },
		// 训练次数
		{ label: t("视训次数"), prop: "trainCount", minWidth: 80 },
		{
			label: t('分层'),
			prop: 'layer',
			minWidth: 200,
            showOverflowTooltip: true,
		},
		// { label: t("预约id"), prop: "appointmentId", minWidth: 120 },
		// {
		// 	label: t("删除时间"),
		// 	prop: "deleteDate",
		// 	minWidth: 140,
		// 	sortable: "custom",
		// 	component: {
		// 		name: "cl-date-text",
		// 		props: { format: "YYYY-MM-DD" },
		// 	},
		// },
		// {
		// 	label: t("创建时间"),
		// 	prop: "createTime",
		// 	minWidth: 170,
		// 	sortable: "desc",
		// 	component: { name: "cl-date-text" },
		// },
		// {
		// 	label: t("更新时间"),
		// 	prop: "updateTime",
		// 	minWidth: 170,
		// 	sortable: "custom",
		// 	component: { name: "cl-date-text" },
		// },
		{ type: "op", buttons: ["edit", "delete"] },
	],
});

// cl-search
const Search = useSearch();

// cl-crud
const Crud = useCrud(
	{
		service: service.student.checkData
	},
	(app) => {
		app.refresh();
	},
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}

async function cleanData() {
	await service.student.checkData.cleanEyeAxisData();
}

async function mergeEyeData() {
	await service.student.checkData.mergeEyeData();
}
</script>
